package cn.shlbysj.demo.controller;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.entity.MakeInfo;
import cn.shlbysj.demo.service.IMakeService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller
@RequestMapping("/make")
@Api("定制模块")
public class MakeController {
    @Autowired
    private IMakeService makeService;

    @RequestMapping(value = "/getListTop",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取前五条的定制记录")
    @ResponseBody
    public Result getListTop(HttpServletRequest request){
        try {
            List<MakeInfo> list= makeService.getListTop();
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(list),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取定制列表")
    @ResponseBody
    public Result list(HttpServletRequest request,
                       @ApiParam(name="name",value="姓名")@RequestParam(value = "name",required = false) String name,
                       @ApiParam(name="state",value="状态")@RequestParam(value = "state",required = false) Integer state,
                       @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                       @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = makeService.findPage(name,state,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "发布定制需求")
    @ResponseBody
    public Result insert(HttpServletRequest request,
                         @ApiParam(name="makeInfo",value="定制信息")@RequestBody MakeInfo makeInfo
    ){
        try {
            return makeService.insert(makeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "修改定制需求")
    @ResponseBody
    public Result update(HttpServletRequest request,
                         @ApiParam(name="makeInfo",value="定制信息")@RequestBody MakeInfo makeInfo
    ){
        try {
            return makeService.update(makeInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
}
